package com.bw.shop2208a.car

import android.location.GnssAntennaInfo.Listener
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.bw.shop2208a.R
import com.bw.shop2208a.adapter.CarAdapter
import com.bw.shop2208a.databinding.FragmentCarBinding
import com.bw.shop2208a.entity.AddcarParam
import com.bw.shop2208a.entity.DeleteCarParam
import com.bw.shop2208a.mvvm.BaseFragment


class CarFragment : BaseFragment<FragmentCarBinding,CarViewModel>() {
    val carAdapter = CarAdapter()
    lateinit var cb :CheckBox
    var i:Int = 0
    override fun initViwe() {
        binding.rvCar.layoutManager = LinearLayoutManager(context)
        binding.rvCar.adapter = carAdapter
        viewModel.getCar()

        carAdapter.addChildClickViewIds(R.id.add,R.id.minus,R.id.delete,R.id.cb)
        carAdapter.setOnItemChildClickListener { adapter, view, position ->
            val carEntity = carAdapter.data[position]
            when(view.id){
                R.id.add ->{
                    viewModel.addCar(AddcarParam(1,carAdapter.data[position].goods_id))
                    val count = carAdapter.getViewByPosition(position, R.id.count) as TextView
                    count.text = ""+(++carAdapter.data[position].count)
                    checkAll()
                }
                R.id.minus ->{
                    if (carAdapter.data[position].count==1){
                        ToastUtils.showLong("您不能在减少了，最小数量为1")
                    }else{
                        viewModel.addCar(AddcarParam(-1,carAdapter.data[position].goods_id))
                        val count = carAdapter.getViewByPosition(position, R.id.count) as TextView
                        count.text = ""+(--carAdapter.data[position].count)
                        checkAll()
                    }
                }
                R.id.delete ->{
                    viewModel.deleteCar(DeleteCarParam("${carAdapter.data[position].id}"))
                    carAdapter.data.removeAt(position)
                    carAdapter.notifyItemRemoved(position)
                    checkAll()
                }
                R.id.cb ->{
                    //修改checkbox的值为原来的相反值
                    cb = carAdapter.getViewByPosition(position,R.id.cb) as CheckBox
                    carAdapter.data[position].isChecked = cb.isChecked
                }
            }
            //无论点击了数量加减、item的删除、还是进行了选中 都需要更新金额变化 、下方全选按钮发生变化
            totalMoney()
            checkAll()
        }
        binding.cbAll.setOnCheckedChangeListener { buttonView, isChecked ->
            binding.cbAll.setOnClickListener {
                for (datum in carAdapter.data) {
                    datum.isChecked = isChecked
                }
                carAdapter.notifyDataSetChanged()
                totalMoney()
            }
        }
    }

    private fun totalMoney() {
        var moeny = 0//合计金额
        for (datum in carAdapter.data) {
            if (datum.isChecked){
                moeny+=datum.count*datum.goods_default_price
            }
        }
        binding.total.text = "合计：$moeny 元"
    }

    private fun checkAll() {
        //用来接收所以数据的选择状态
        val list = mutableListOf<Boolean>()
        carAdapter.data.forEach {
            list.add(it.isChecked)
        }
        binding.cbAll.isChecked = false !in list
    }

    override fun iniData() {
        viewModel.successCar.observe(this){
            if (it.code==200){
                carAdapter.data.clear()
                carAdapter.data.addAll(it.data)
                carAdapter.notifyDataSetChanged()
            }
        }
        viewModel.successAddCar.observe(this){
        }
        viewModel.successDeleteCar.observe(this){
            ToastUtils.showLong(it.message)
        }
        viewModel.failDeleteCar.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun getLayoutId(): Int = R.layout.fragment_car

    override fun getViewModel(): Class<CarViewModel> = CarViewModel::class.java

}